package cn.sirun.com.fca.account.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.tencent.cloud.iov.util.BitmapUtils;
import com.tencent.cloud.iov.util.ViewStateUtils;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.RequestParamConst;
import cn.sirun.com.fca.account.listener.OnBackPressedListener;
import cn.sirun.com.fca.account.uitl.AccountViewUtils;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OperateFragment.OnOperateListener} interface
 * to handle interaction events.
 * Use the {@link OperateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class OperateFragment extends Fragment
        implements View.OnClickListener, OnBackPressedListener {
    private static final String PICTURE_TYPE = "picture_type";

    private static final String FILE_PATH = "file_path";

    private static final int SAMPLE_IMG_NUMBER = 4;

    private static final int[] VEHICLE_SAMPLE_IMG_RES_ID = new int[]{
            R.drawable.act_btn_enter_disabled,
            R.drawable.act_btn_enter_disabled,
            R.drawable.act_btn_enter_disabled,
            R.drawable.act_btn_enter_disabled};

    private static final int[] IDENTITY_SAMPLE_IMG_RES_ID = new int[]{
            R.drawable.act_btn_enter_disabled,
            R.drawable.act_btn_enter_disabled,
            R.drawable.act_btn_enter_disabled,
            R.drawable.act_btn_enter_disabled};

    private RequestParamConst.PictureTypeEnum mPictureType;

    private OnOperateListener mListener;

    private TextView mUploadTitleTV;

    private ImageView mImgIDView;

    private ImageView mTakePhotoIV;

    private ImageView[] mSampleImgIVs;

    private RelativeLayout mUploadingInfoRL;

    private LinearLayout mReselectSubmitLL;

    private Button mManualInputBtn;

    private Button mReselectBtn;

    private Button mSubmitBtn;

    private String mImgIDFilePath;

    private ImageView mImgIdFront,mImgIdcardVerso,mImgIdcardInhand;

    private TextView mTvnext;

    public OperateFragment() {
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param pictureType
     * @param filePath
     * @return A new instance of fragment OperateFragment.
     */
    public static OperateFragment newInstance(RequestParamConst.PictureTypeEnum pictureType,
                                              String filePath) {
        OperateFragment fragment = new OperateFragment();
        Bundle args = new Bundle();
        args.putString(PICTURE_TYPE, pictureType.name());
        if (!TextUtils.isEmpty(filePath)) {
            args.putString(FILE_PATH, filePath);
        }
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != getArguments()) {
            mPictureType = RequestParamConst.PictureTypeEnum.valueOf(
                    getArguments().getString(PICTURE_TYPE));
            mImgIDFilePath = getArguments().getString(FILE_PATH);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.operate_fragment, container, false);

        mUploadingInfoRL = view.findViewById(R.id.rl_uploading_info);
        mReselectSubmitLL = view.findViewById(R.id.ll_reselect_submit);
        mUploadTitleTV = view.findViewById(R.id.tv_title_upload);
        mImgIDView = view.findViewById(R.id.iv_img_id);
        mTakePhotoIV = view.findViewById(R.id.iv_take_photo);
        mManualInputBtn = view.findViewById(R.id.btn_manual_input);
        mReselectBtn = view.findViewById(R.id.btn_reselect);
        mSubmitBtn = view.findViewById(R.id.btn_submit);
        mImgIdFront = view.findViewById(R.id.img_idcard_front);
        mImgIdcardVerso = view.findViewById(R.id.img_idcard_verso);
        mImgIdcardInhand = view.findViewById(R.id.img_idcar_inhand);
        mTvnext = view.findViewById(R.id.tv_next);

        AccountViewUtils.adjustTitleMargin(mUploadTitleTV);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mTvnext.setOnClickListener(this);
        mImgIDView.setOnClickListener(this);
        mImgIdcardVerso.setOnClickListener(this);
        mImgIdcardInhand.setOnClickListener(this);
        mManualInputBtn.setOnClickListener(this);
        mReselectBtn.setOnClickListener(this);
        mSubmitBtn.setOnClickListener(this);

        mImgIdFront.setOnClickListener(this);

        mSampleImgIVs = new ImageView[SAMPLE_IMG_NUMBER];
        mSampleImgIVs[0] = view.findViewById(R.id.iv_img_right);
        mSampleImgIVs[1] = view.findViewById(R.id.iv_img_frame_error);
        mSampleImgIVs[2] = view.findViewById(R.id.iv_img_vague);
        mSampleImgIVs[3] = view.findViewById(R.id.iv_img_too_bright);

        ViewStateUtils.setUpViewPressState(mTakePhotoIV);
        init();
    }

    @Override
    public void onStart() {
        super.onStart();
        updateView("3");
    }

    @Override
    public void onClick(View v) {
        if (null == mListener) {
            return;
        }
        int id = v.getId();
        if (R.id.btn_reselect == id) {
            mImgIDFilePath = "";
            updateView("3");
        } else if (R.id.btn_submit == id) {
            mUploadingInfoRL.setVisibility(View.VISIBLE);
            setButtonClickable(false);
            mListener.onSubmitCallback(mImgIDFilePath);
        } else if (R.id.iv_img_id == id) {
            mListener.onTakePhotoCallback();
        } else if (R.id.btn_manual_input == id) {
//            mListener.onManualInputCallback();
        } else if (R.id.img_idcard_front == id) {
            mListener.onTakePhotoCallback();
        }else if (R.id.img_idcard_verso == id) {
            mListener.onTakePhotoCallbackTwo();
        }else if (R.id.img_idcar_inhand == id) {
            mListener.onTakePhotoCallbackThree();
        }else if (R.id.tv_next == id) {
            mListener.onManualInputCallback(mTvnext);
        }
    }

    @Override
    public boolean onBackPressed() {
        if (View.VISIBLE == mUploadingInfoRL.getVisibility()) {
            return true;
        }
        return false;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * ctivity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnOperateListener {
        void onManualInputCallback(TextView textView);

        void onTakePhotoCallback();

        void onTakePhotoCallbackTwo();

        void onTakePhotoCallbackThree();

        void onSubmitCallback(String filePath);
    }

    public void setOperateListener(OnOperateListener listener) {
        mListener = listener;
    }

    public void setFilePath(String filePath) {
        mImgIDFilePath = filePath;
    }

    public String getFilePath(){

        return mImgIDFilePath;
    }

    public void setImageView(String filePath) {
        setFilePath(filePath);
        if (null != mImgIDView) {
            updateView("3");
        }
    }

    public void setButtonClickable(boolean isClickable) {
        mReselectBtn.setClickable(isClickable);
        mSubmitBtn.setClickable(isClickable);
        mImgIDView.setClickable(isClickable);
        if (isClickable) {
            mUploadingInfoRL.setVisibility(View.GONE);
        }
    }

    public void setPictureType(RequestParamConst.PictureTypeEnum pictureType) {
        mPictureType = pictureType;
    }

    public void updateView(String mFlagPicUrl) {
//        mImgIDView.post(new Runnable() {
//            @Override
//            public void run() {
//                if (!TextUtils.isEmpty(mImgIDFilePath)) {
//                    Bitmap bitmap = BitmapFactory.decodeFile(mImgIDFilePath);
//                    bitmap = BitmapUtils.zoomBitmap(bitmap, mImgIDView.getWidth(),
//                            mImgIDView.getHeight());
//                    mImgIDView.setImageBitmap(bitmap);
//                } else {
//                    mImgIDView.setImageResource(R.drawable.act_btn_enter_disabled);
//                }
//            }
//        });

        if(mFlagPicUrl.equals("0")){
            mImgIdFront.post(new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(mImgIDFilePath)) {
                        Bitmap bitmap = BitmapFactory.decodeFile(mImgIDFilePath);
                        bitmap = BitmapUtils.zoomBitmap(bitmap, mImgIdFront.getWidth(),
                                mImgIdFront.getHeight());
                        mImgIdFront.setImageBitmap(bitmap);
                    } else {
                        /**
                         * 默认图片
                         */
                        mImgIdFront.setImageResource(R.drawable.ic_own_auth_default);
                    }
                }
            });
        }else if(mFlagPicUrl.equals("1")){
            mImgIdcardVerso.post(new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(mImgIDFilePath)) {
                        Bitmap bitmap = BitmapFactory.decodeFile(mImgIDFilePath);
                        bitmap = BitmapUtils.zoomBitmap(bitmap, mImgIdcardVerso.getWidth(),
                                mImgIdcardVerso.getHeight());
                        mImgIdcardVerso.setImageBitmap(bitmap);
                    } else {
                        /**
                         * 默认图片
                         */
                        mImgIdcardVerso.setImageResource(R.drawable.ic_own_auth_default);
                    }
                }
            });
        }else if(mFlagPicUrl.equals("2")){
            mImgIdcardInhand.post(new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(mImgIDFilePath)) {
                        Bitmap bitmap = BitmapFactory.decodeFile(mImgIDFilePath);
                        bitmap = BitmapUtils.zoomBitmap(bitmap, mImgIdcardInhand.getWidth(),
                                mImgIdcardInhand.getHeight());
                        mImgIdcardInhand.setImageBitmap(bitmap);
                    } else {
                        /**
                         * 默认图片
                         */
                        mImgIdcardInhand.setImageResource(R.drawable.ic_own_auth_default);
                    }
                }
            });
        }







        if (!TextUtils.isEmpty(mImgIDFilePath)) {
            mReselectSubmitLL.setVisibility(View.GONE);

            mTakePhotoIV.setVisibility(View.GONE);
            mManualInputBtn.setVisibility(View.GONE);
        } else {
            mUploadingInfoRL.setVisibility(View.GONE);
            mReselectSubmitLL.setVisibility(View.GONE);

            mTakePhotoIV.setVisibility(View.GONE);
            mManualInputBtn.setVisibility(View.GONE);
        }
    }




    public void updateTextBg(String flagBg){
        if(flagBg.equals("0")){
            mTvnext.setBackgroundResource(R.drawable.s_btn_bg_border);
        }else{

        }
    }

    private void init() {
        if (RequestParamConst.PictureTypeEnum.VEHICLE_LICENSE == mPictureType) {
            mUploadTitleTV.setText("sdfd");
            for (int i = 0; i < SAMPLE_IMG_NUMBER; ++i) {
                mSampleImgIVs[i].setImageResource(VEHICLE_SAMPLE_IMG_RES_ID[i]);
            }
        } else {
            mUploadTitleTV.setText("sdfd");
            for (int i = 0; i < SAMPLE_IMG_NUMBER; ++i) {
                mSampleImgIVs[i].setImageResource(IDENTITY_SAMPLE_IMG_RES_ID[i]);
            }
        }
    }

}
